home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
VRML Browsing & Building Cyberspace
/
VRML - Browsing and Building Cyberspace.iso
/
txt2wrl
/
txt2wrl.cxx
Wrap
C/C++ Source or Header
|
1995-08-03
|
8KB
|
313 lines
/*
Copyright Layne Thomas(lthomas@cs.uah.edu) April 23,1995
this code is a sloppy hack but it works
This source is subject to the terms and stuff of the GNU public license.
*******IMPORTANT STUFF*********
Code reads in a txt file, and outputs the 3d representation in coord3 format
usage: txt2wrl <infilename> <z height(default is 2)>
output is to stdout
Compile with : g++ txt2wrl.cxx -o txt2wrl
note: txt2wrl txt files have to have a blank line as the first line in the file!
This is cuz I'm a lazy butt and should change it in the future
Go to http://cs.uah.edu/~lthomas/vrml for more information
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
unsigned int matrix_size;
int yunit,xunit;
unsigned int *real_matrix;
int points;
int pointsb;
int Z=2;
unsigned matrix(int x, int y){
unsigned result=real_matrix[(unsigned int)(x+(y*xunit))];
return result;
}
void set_matrix(int x, int y,unsigned int value){
real_matrix[(unsigned int)(x+(y*xunit))]=value;
}
void clean(char *dastring,unsigned int length){
int cx;
for (cx=0;cx<length;cx++){
dastring[cx]=' ';
}
}
void get_line(FILE *infile, char *dest){
int c=0;
dest[c]=0;
while((dest[c-1] != '\n')&&(!feof(infile))){
fscanf(infile,"%c",&dest[c]);
//Detect tabs(8 spaces to a tab)
if (dest[c]==9) {
dest[c]=' ';
c+=7;
}
c++;
}
if (feof(infile)) dest[c+1]=0;
else dest[c]=0;
}
int get_ranges(char *filename,int &lines,int &xrange,int &zrange){
FILE *infile;
char crap[2048];
int longestline;
if ((infile = fopen(filename, "rt"))== NULL){
printf("Error opening %s\n",filename);
return 1;
}
lines=0;
longestline=0;
while (!feof(infile)) {
get_line(infile,crap);
if (strlen(crap) > longestline) {
longestline=strlen(crap);
}
lines++;
}
fclose(infile);
xrange=longestline;
zrange=lines;
return 0;
}
int txt2rem(char *filename){
FILE *infile;
char crap[2048];
if ((infile = fopen(filename, "rt"))== NULL){
printf("Error opening %s\n",filename);
return 0;
}
clean(crap,2048);
while (!feof(infile)) {
get_line(infile,crap);
printf("#%s",crap);
}
fclose(infile);
return 0;
}
void parse_line(char *data,int curline,int xrange,int zrange){
int x;
int first=1;
if (strlen(data)==0) return;
for (x=0;x<strlen(data);x++) {
if ((data[x] != ' ')&&(data[x]!='\n')) {
set_matrix(x,curline,1);
}
else {
set_matrix(x,curline,0);
}
}
}
void clear_matrix(){
unsigned int x;
for (x=0;x<matrix_size/4;x++){
// printf("X=%d\n",x);
real_matrix[x]=0;
}
}
void print_matrix(int xrange,int zrange){
int x,y;
for (y=0;y<zrange;y++) {
printf("\n#");
// printf("\n #(%d)",y);
for (x=0;x<xrange;x++) {
if (matrix(x,y)==0) printf(" ");
else if (matrix(x,y)==1) printf("+");
else printf("@");
// printf("%d",matrix(x,y));
}
}
}
void print_line(int xrange, int line) {
int x;
printf("\n #(%d)",line);
for (x=0;x<xrange;x++) {
printf("%d",matrix(x,line));
}
}
int horizontal_line(int x,int y){
if ((matrix(x+1,y)!=0)&&(matrix(x-1,y)!=0)) {
//Is horizontal, but is it just a line or a junction?
if ((matrix(x,y-1)!=0)||(matrix(x,y+1)!=0)) return 0;
return 1;
}
return 0;
}
int vertical_line(int x,int y){
if ((matrix(x,y+1)!=0)&&(matrix(x,y-1)!=0)){
if ((matrix(x-1,y)!=0)||(matrix(x+1,y)!=0)) return 0;
return 1;
}
return 0;
}
int is_point(int x,int y){
if (vertical_line(x,y)) return 0;
if (horizontal_line(x,y)) return 0;
return 1;
}
int get_points(int xrange, int zrange){
int x,y,count;
count=2; //Skip 0 cuz 0=empty,1=wall
for (y=0;y<zrange;y++){
for (x=0;x<xrange;x++){
if (matrix(x,y)!=0) {
if (is_point(x,y)) {
set_matrix(x,y,count);
printf("\t\t%d %d %d,\n",x,0,y);
printf("\t\t%d %d %d,\n",x,Z,y);
count+=2;
} else {
set_matrix(x,y,1);
}
} else {
set_matrix(x,y,0);
}
}
}
}
int build_hor_walls(int xrange, int zrange){
int x,y;
int node1,node2;
for (y=0;y<zrange;y++){
for (x=0;x<xrange;x++){
if ((matrix(x,y)>1)&&((matrix(x+1,y)!=0)||(matrix(x-1,y)!=0))){
node1=x;
while (matrix(x+1,y)>0) {
x++;
node2=x;
}
printf("\t\t%d,%d,%d,%d,-1,\n",matrix(node1,y)-2,matrix(node1,y)+1-2,
matrix(node2,y)+1-2,matrix(node2,y)-2);
}
}
}
}
int build_ver_walls(int xrange, int zrange){
int x,y;
int node1,node2;
for (x=0;x<xrange;x++){
for (y=0;y<zrange;y++){
if ((matrix(x,y)>1)&&((matrix(x,y+1)!=0)||(matrix(x,y-1)!=0))){
node1=y;
while (matrix(x,y+1)>0) {
y++;
node2=y;
}
// printf("Line (%d,%d)(%d)-(%d,%d)(%d) matrix is %d\n",x,node1,matrix(x,node1),x,node2,matrix(x,node2),matrix(x,y));
printf("\t\t%d,%d,%d,%d,-1,\n",matrix(x,node1)-2,matrix(x,node1)+1-2,
matrix(x,node2)+1-2,matrix(x,node2)-2);
}
}
}
}
int main(int argc,char *argv[]) {
char *data=(char *)malloc(2048);
char *filename=(char *)malloc(1024);
int lines,zrange,xrange;
int cur_line;
FILE *infile;
if (argc != 3) {
printf("usage: txt2wrl <infilename> <z height(default 2)>\n");
return 0;
}
strcpy(filename,argv[1]);
get_ranges(filename,lines,xrange,zrange);
if (atoi(argv[2]) != 0) Z=atoi(argv[2]);
printf("#VRML V1.0 ascii\n");
printf("#There are %d lines, ",lines);
printf("X range is %d to %d, ",-xrange/2,xrange/2);
printf("Z range is %d to %d\n",-zrange/2,zrange/2);
yunit=zrange;
xunit=xrange;
matrix_size=(xrange)*(zrange)*2+(xrange*2);//2 for int, other for extra
real_matrix=(unsigned int *)malloc(matrix_size*2);
if (real_matrix==NULL) {
printf("Not enough memory to initialize matrix\n");
return -1;
}
printf("#Matrix %p,size is %d(yunit=%d)\n",real_matrix,matrix_size,yunit);
clear_matrix();
printf("#opening %s\n",filename);
txt2rem(filename);
printf("\n");
printf("Separator {\n");
printf(" Scale { scaleFactor 1.0 1.0 3.2 }\n");
printf(" Material { emissiveColor 0.3 0.3 0.0 }\n");
printf(" Coordinate3 {\n");
printf(" point [\n");
if ((infile = fopen(filename, "rt"))== NULL){
printf("Error opening %s\n",filename);
}
clean(data,2048);
points=0;
pointsb=0;
cur_line=0;
//Get informational data
printf("#parsing\n");
while (!feof(infile)){
get_line(infile,data);
parse_line(data,cur_line,xrange,zrange);
cur_line++;
}
get_points(xrange,zrange);
printf(" 0 0 0]\n");
print_matrix(xrange,cur_line);
fclose(infile);
printf("\n }\n");
printf(" IndexedFaceSet {\n");
printf(" coordIndex [ \n");
build_hor_walls(xrange,zrange);
printf("\n# Vertical walls\n");
build_ver_walls(xrange,zrange);
printf("\n\t\t0,0,0,0,-1 ] \n");
printf(" }\n");
printf("}\n");
return 0;
}